import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:38917
 * Date:2025/4/12
 * Time:16:32
 */

public class Solution {
    public int[] productExceptSelf(int[] nums) {
        int[] f = new int[nums.length];
        int[] g = new int[nums.length];
        f[0] = 1;
        g[g.length - 1] = 1;
        //前缀积
        for (int i = 1; i < f.length; i++) {
            f[i] = f[i - 1] * nums[i - 1];
        }
        //后缀积
        for (int i = g.length - 2; i >= 0; i--) {
            g[i] = g[i + 1] * nums[i + 1];
        }
        int[] ret = new int[nums.length];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = f[i] * g[i];
        }
        return ret;
    }
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] ret = solution.productExceptSelf(new int[]{-1, 1, 0, -3, 3});
        System.out.println(Arrays.toString(ret));
    }
}
